package admin

import (
	"exercise/application/yiran/app/libs"
	"exercise/application/yiran/app/request/adminRequest"
	"exercise/application/yiran/app/service/admin"
	"github.com/gin-gonic/gin"
)

type examTimeController struct{}

// List 列表
func (*examTimeController) List(ctx *gin.Context) {
	request := new(adminRequest.ExamTimeListRequest)
	err := libs.BindRequest(ctx, request)
	if err != nil {
		libs.Toast(ctx, err.Error())
		return
	}

	ret, err := admin.NewExamTimeService().List(ctx, request)
	if err != nil {
		libs.Toast(ctx, err.Error())
		return
	}

	libs.Success(ctx, ret)
}

// Add 新增
func (*examTimeController) Add(ctx *gin.Context) {
	request := new(adminRequest.AddExamTimeRequest)
	err := libs.BindRequest(ctx, request)
	if err != nil {
		libs.Toast(ctx, err.Error())
		return
	}

	ret, err := admin.NewExamTimeService().Add(ctx, request)
	if err != nil {
		libs.Toast(ctx, err.Error())
		return
	}
	libs.Success(ctx, ret)
}

// Edit 编辑
func (*examTimeController) Edit(ctx *gin.Context) {
	request := new(adminRequest.EditExamTimeRequest)
	err := libs.BindRequest(ctx, request)
	if err != nil {
		libs.Toast(ctx, err.Error())
		return
	}

	err = admin.NewExamTimeService().Edit(request)
	if err != nil {
		libs.Toast(ctx, err.Error())
		return
	}
	libs.OkToast(ctx, "编辑成功")
}

// Detail 详情
func (*examTimeController) Detail(ctx *gin.Context) {
	ret, err := admin.NewExamTimeService().Detail(ctx)
	if err != nil {
		libs.Toast(ctx, err.Error())
		return
	}
	libs.Success(ctx, ret)
}

// Deleted 删除
func (*examTimeController) Deleted(ctx *gin.Context) {
	err := admin.NewExamTimeService().Deleted(ctx)
	if err != nil {
		libs.Toast(ctx, err.Error())
		return
	}
	libs.OkToast(ctx, "删除成功")
}

func (rev *examTimeController) AddCache(ctx *gin.Context) {
	err := admin.NewExamTimeService().AddCache(ctx)
	if err != nil {
		libs.Toast(ctx, err.Error())
		return
	}
	libs.OkToast(ctx, "缓存成功")
}

// CopyTime 复制考次
func (rev examTimeController) CopyTime(ctx *gin.Context) {
	err := admin.NewExamTimeService().CopyTime(ctx)
	if err != nil {
		libs.Toast(ctx, err.Error())
		return
	}
	libs.OkToast(ctx, "操作成功")
}

func NewExamTimeController() *examTimeController {
	return &examTimeController{}
}
